package com.example.demo.controller;

import com.example.demo.entity.Role;
import com.example.demo.entity.UserMsg;
import com.example.demo.mapper.RoleMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

/**
 * @program: demo
 * @description: 角色控制层
 * @author: Zwill
 * @create: 2019-12-25 15:28
 **/

@Api(tags = "role")
@RestController
@EnableSwagger2
public class RoleController {

    @Autowired
    private RoleMapper roleMapper;

    /*i.角色不能重复（校验）添加角色*/
    @ApiOperation("角色添加")
    @PostMapping("/addrole")
    public int addRole(Role role) {
        List<String> rolenames = roleMapper.findAllRoleNames();
        if (role != null) {
            if (role.getRolename() != null && !rolenames.contains(role.getRolename())) {
                return roleMapper.insertRole(role);
            }
            System.out.println("角色为空或者已经存在！");
        }
        return 0;
    }

    /*ii.如果角色当中有人员信息，则不允许删除*/
    @ApiOperation("角色删除")
    @PostMapping("/deleterole")
    public int DeleteRole(String rolename) {
        int roleNums = roleMapper.findRoleNums(rolename);
        if (roleNums == 0) {
            roleMapper.DeleteRole(rolename);
        } else {
            System.out.println("角色中有人员信息不允许删除！");
        }
        return 0;
    }

    /*iii.查询条件：角色名称模糊查询*/
    @ApiOperation("角色模糊查询")
    @PostMapping("/roleFuzzyQueryByName")
    public List<Role> roleFuzzyQueryByName(String fuzzyRoleName) {
        return roleMapper.roleFuzzyQueryByName(fuzzyRoleName);
    }

    /*iv.显示列表：角色名称、此角色下共计人数*/
    @ApiOperation("显示列表")
    @PostMapping("/showRoleAndNums")
    public List<Role> showRoleAndNums() {
        return roleMapper.showRoleAndNums();
    }

    /*查询角色*/
    @ApiOperation("查询角色")
    @PostMapping("/selectRole")
    public List<Role> selectRole(){
        return roleMapper.selectRole();
    }

    /*修改角色信息*/
    @ApiOperation("修改角色信息")
    @PostMapping("/updateRole")
    public int updateRole(Role role){
        return roleMapper.updateRole(role);
    }
}
